package com.xiaobai.redisContact.config;


//import com.xiaobai.redisContact.listener.RedisUpdateAndAddListener;
import com.xiaobai.redisContact.listener.RedisUpdateAndAddListener;
import jakarta.annotation.Resource;
import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;

@Configuration
public class RedisConfig {

    @Value("${spring.data.redis.host}")
    private String redisHost;

    @Value("${spring.data.redis.port}")
    private Integer redisPort;

    @Value("${spring.data.redis.timeout}")
    private Integer redisTimeout;

//    @Value("${spring.redis.password}")
//    private String redisPassword;


//    @Resource
//    private RedisUpdateAndAddListener redisUpdateAndAddListener;
//    @Resource
//    private RedisDeleteListener redisDeleteListener;
//    @Resource
//    private RedisHashUpdateAndAddListener redisHashUpdateAndAddListener;
//    @Resource
//    private RedisListUpdateAndAddListener redisListUpdateAndAddListener;
//    @Resource
//    private RedisSetUpdateAndAddListener redisSetUpdateAndAddListener;
//    @Resource
//    private RedisSortUpdateAndAddListener redisSortUpdateAndAddListener;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(8);
        poolConfig.setMaxIdle(2);
        poolConfig.setMinIdle(0);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(false);
        poolConfig.setTestWhileIdle(true);

        JedisClientConfiguration clientConfig = JedisClientConfiguration.builder()
            .usePooling().poolConfig(poolConfig).and().readTimeout(Duration.ofMillis(redisTimeout)).build();

        // 单点redis
        RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
        redisConfig.setHostName(redisHost);
        //redisConfig.setPassword(RedisPassword.of(redisAuth));
        redisConfig.setPort(redisPort);
        redisConfig.setDatabase(0);
        return new JedisConnectionFactory(redisConfig,clientConfig);

    }

    //简单序列化
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        // 设置键序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置简单类型值的序列化方式
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        // 设置默认序列化方式
        redisTemplate.setEnableTransactionSupport(true); // 支持事务
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    //    //加载lua脚本，设置返回值类型
//    @Bean
//    public RedisScript<Boolean> script() {
//        Resource scriptSource = new ClassPathResource("lua/test.lua");
//        return RedisScript.of(scriptSource, Boolean.class);
//    }
    @Resource
    private RedisUpdateAndAddListener redisUpdateAndAddListener;


    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory());
        //监听所有的key的set事件
//	      '__key*__:*'#对所有库键空间通知
//	      '__keyspace@0__:*' #是对db0数据库键空间通知
//	      '__keyspace@0__:test*' #是对db0数据库，key前缀为test所有键的键空间通知
        container.addMessageListener(redisUpdateAndAddListener, new PatternTopic("__keyevent@0__:test"));
        System.out.println("监听事件启动！----------------------------------");
        return container;
    }
}
